package com.ruoyi.system.service.vantservice.loginservice.impl;

import com.ruoyi.system.domain.customer.CustomerInfo;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.mapper.customer.CustomerInfoMapper;
import com.ruoyi.system.mapper.vant.login.ApiLoginMapper;
import com.ruoyi.system.service.vantservice.loginservice.ApiLoginService;
import com.ruoyi.system.utils.TokenUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.sign.Md5Utils;

import java.util.concurrent.TimeUnit;

@Service
public class ApiLoginServiceImpl implements ApiLoginService {

    @Autowired
    private ApiLoginMapper loginMapper;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private TokenUtils tokenUtils;



    /**
     * 校验用户用户名和密码
     * @param customerLogin
     * @return
     */
    @Override
    public String checkLoginCustomer(CustomerLogin customerLogin){
        // 将密码加密
        String password = Md5Utils.hash(customerLogin.getPassword());
        // 添加到实体类中
        customerLogin.setPassword(password);
        CustomerLogin login = loginMapper.checkLoginCustomer(customerLogin);
        if (login != null) {
            if(login.getUserStats().equals("1")){
                return "1";
            }
            // 生成Token
            String token = tokenUtils.createToken(login);
            return token;
        }else {
            return "2";
        }
    }

    /**
     * 添加用户信息
     * @param customerLogin
     * @return
     */
    public String addCustomer(CustomerLogin customerLogin){
        // 根据登录手机号查询
        CustomerLogin login = loginMapper.checkLoginCustomer(customerLogin);
        // 已有账号信息则不添加数据库
        if (login != null) {
            if(login.getUserStats().equals("1")){
                return "1";
            }
            // 生成Token
            String token = tokenUtils.createToken(login);
            return token;
        }else{
            // 将密码加密
            String password = Md5Utils.hash(customerLogin.getPassword());
            // 添加到实体类中
            customerLogin.setPassword(password);
            if (customerLogin.getInviterId() == null || "".equals(customerLogin.getInviterId())) {
                customerLogin.setInviterId(0);
            }
            int count = loginMapper.addCustomer(customerLogin);
            Long customerId = customerLogin.getCustomerId();
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.setCustomerId(customerId.intValue());
            customerInfoMapper.insertCustomerInfo(customerInfo);
            if (count > 0) {
                // 生成Token
                String token = tokenUtils.createToken(customerLogin);
                return token;
            }else {
                return "2";
            }
        }
    }

    /**
     * 校验用户信息是否存在
     * @param customerLogin
     * @return
     */
    public String checkLoginPhone(CustomerLogin customerLogin){
        CustomerLogin login = loginMapper.selectCustomerByName(customerLogin.getLoginName());
        if( login != null){
            if(login.getUserStats().equals("1")){
                return "1";
            }
            // 生成Token
            String token = tokenUtils.createToken(login);
            return token;
        }
        return "2";
    }

    /**
     * 修改密码
     * @param customerLogin
     * @return
     */
    public int updateCustomer(CustomerLogin customerLogin){
        CustomerLogin login = loginMapper.selectCustomerByName(customerLogin.getLoginName());
        // 将密码加密
        String password = Md5Utils.hash(customerLogin.getPassword());
        login.setPassword(password);
        return loginMapper.updateCustomer(login);
    }


}
